<?php
namespace app\common\model;

class WebConfig extends \think\Model
{
    private $table_name = 'hn_avigation';
    /** 封装一个菜单对象 */
    public function fillNavigation($id, $isshow, $title, $url, $sort, $group)
    {
        $model = ['id' => $id, 'group' => $group, 'title' => $title, 'url' => $url, 'sort' => $sort, 'show' => $isshow];
        return $model;
    }
    /** 获取一个菜单信息 */
    public function getNavInfo($id)
    {
        $model = \think\Db::table($this->table_name)->where('id', $id)->find();
        return $model;
    }
    /** 返回导航菜单列表 */
    public function getNavList($group, $show)
    {
        $query = \think\Db::table($this->table_name);
        if (\strlen($show)) {
            $query = $query->where('show', $show);
        }
        if (\strlen($group)) {
            $query = $query->where('group', $group);
        }
        $lst = $query->select();
        return $lst;
    }
    /** 新增一个菜单内容 */
    public function createNavigation($model)
    {
        if (strlen($model['sort']) < 1) {
            $model['sort'] = 0;
        }
        if (strlen($model['show']) < 1) {
            $model['show'] = 0;
        }

        $rule = [
            'group' => 'require|max:20',
            'title' => 'require|max:50',
            'url' => 'require|max:128',
            'sort' => 'require|number',
            'show' => 'require|number'
        ];
        $message = [];

        $validate = new \think\Validate($rule, $message);

        if ($validate->check($model)) {
            $model['id'] = \uniqid();

            $res = \think\Db::table($table_name)->insert($model);

            if ($res > 0) {
                return ['flag' => \TRUE, 'data' => $model['id']];
            }
            else {
                return ['flage' => \FALSE, 'msg' => '数据库操作失败!'];
            }
        }
        else {
            return ['flag' => \FALSE, 'msg' => $validate->getError()];
        }
    }
    /** 修改菜单的内容 */
    public function updateNavigation($id, $newmodel)
    {
        $model = $this->getNavInfo($id);
        if (is_null($model)) {
            return ['flag' => \FALSE, 'msg' => '没有此导航菜单'];
        }

        if ($model['group'] != $new_info['group'] && strlen($new_info['group']) > 1) {
            $model['group'] = $new_info['group'];
        }
        if ($model['title'] != $new_info['title'] && strlen($new_info['title']) > 1) {
            $model['title'] = $new_info['title'];
        }
        if ($model['url'] != $new_info['url'] && strlen($new_info['url']) > 1) {
            $model['url'] = $new_info['url'];
        }
        if ($model['sort'] != $new_info['sort'] && strlen($new_info['sort']) > 1) {
            $model['sort'] = $new_info['sort'];
        }
        if ($model['show'] != $new_info['show'] && strlen($new_info['show']) > 1) {
            $model['show'] = $new_info['show'];
        }

        $rule = [
            'group' => 'require|max:20',
            'title' => 'require|max:50',
            'url' => 'require|max:128',
            'sort' => 'require|number',
            'show' => 'require|number'
        ];
        $message = [];

        $validate = new \think\Validate($rule, $message);

        if ($validate->check($model)) {
            $res = \think\Db::table($table_name)->where('id', $id)->update($model);

            if ($res > 0) {
                return ['flag' => \TRUE, 'data' => $model];
            }
            else {
                return ['flage' => \FALSE, 'msg' => '数据库操作失败!'];
            }
        }
        else {
            return ['flag' => \FALSE, 'msg' => $validate->getError()];
        }
    }
    /** 删除菜单对象 */
    public function removeNavigation($id)
    {
        $model = $this->getNavInfo($id);
        if (is_null($model)) {
            return ['flag' => \FALSE, 'msg' => '没有此导航菜单'];
        }
        $model['show'] = 0;
        $res = \think\Db::table($table_name)->where('id', $id)->update(['show' => 0]);

        if ($res > 0) {
            return ['flag' => \TRUE, 'data' => $model];
        }
        else {
            return ['flage' => \FALSE, 'msg' => '数据库操作失败!'];
        }
    }
}